from time import sleep
from selenium.webdriver.support.select import Select
from conf.env import url
class BasePage():
    def __init__(self, driver):
        self.driver = driver
    # 打开项目地址  ,并且设置隐式等待
    def open(self, url, timeout):
        self.driver.get(url)
        self.driver.maximize_window()
        self.driver.implicitly_wait(timeout)
    # 获取 元素定位方法
    def converter(self, target):  # (By.ID,KW)  target = "id=kw" 'xpath=XXXXX'
        by, value = target.split('=', 1)
        return by.strip(), value.strip()  # strip() 去除by 和 value 左右两边的空格
    def locate_element(self, target):
        by, value = self.converter(target)
        
        ele = self.driver.find_element(by, value)
        return ele
    def input(self, target, value):
        self.locate_element(target).send_keys(value)
    def button(self, target):
        self.locate_element(target).click()
    def switch_to_frame(self, frame_locator):
        """
        切换到指定的iframe框架
        :param frame_locator: 框架的定位方式（可以是id/name/index/WebElement）
        """
        self.driver.switch_to.frame(frame_locator)
    def switch_to_default_content(self):
        """切换回主文档（最外层）"""
        self.driver.switch_to.default_content()
    def select_by_text(self, target, text):
        ele = self.locate_element(target)
        select_object = Select(ele)
        select_object.select_by_visible_text(text)
    def select_by_index(self, target, index):
        ele = self.locate_element(target)
        select_object = Select(ele)
        select_object.select_by_index(index)
    def select_by_value(self, target, value):
        ele = self.locate_element(target)
        select_object = Select(ele)
        select_object.select_by_value(value)
    def wait(self, secend):
        sleep(secend)
    
    def quit(self):
        self.driver.quit()
    def get_text(self, target):
        return self.locate_element(target).text